(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.tc.listController', listController);
	listController.$inject = [ '$scope', '$q', '$filter', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'tcService' ];
	function listController($scope, $q, $filter, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, tcService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("tcList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.docMimeTypeOptions = tcService.getDocMimeTypeOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
			
			ctrl.tcHistoryLookup = function(){
				if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
					modalService.openErrorDialogs({
						messages : [ 'title.dialogs.info.norecsel' ]
					});
					return;
				}
				modalService.openCustomDialogs({
				   'dialogsModule' : 'cda.tcHistoryLookup',
				   'params':{
					   'selectedRows' : ctrl.selectedRows,
				    }
				});
			};
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.tc.view', ctrl.selectedRows[0]);
		};

		ctrl.create = function() {
			$scope.go('cda.tc.create');
		};

		ctrl.update = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.tc.update', ctrl.selectedRows[0]);
		};

		ctrl.delete = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.tc.delete', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};
		
		ctrl.resetUsers = function() {//$filter('translate')(label)
			modalService.openConfirmDialogs({
				'messages' : [ $filter('translate')('message.tc.resetusers') ],
				'btnCallback' : {
					'ok' : function() {
						authService.resetUsers().then(resetUsersSuccess, resetUsersFailed);
					}
				}
			});
		};
		
		function resetUsersSuccess(response) {
			modalService.openInfoDialogs({
				'messages' : [ $filter('translate')('message.tc.resetuserssuccess') ],
				'btnCallback' : {
					'close' : function() {
						return;
					}
				}
			});
		}
		
		function resetUsersFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);